package com.zhongwang.business.module.baseCostManage.controller;
import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.constant.Constant;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.model.InputMachiningModel;
import com.zhongwang.business.module.baseCostManage.model.InputOutsourcingModel;
import com.zhongwang.business.module.baseCostManage.service.InputMachiningService;
import com.zhongwang.business.module.baseCostManage.service.InputOutsourcingService;
import com.zhongwang.business.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  控制层
 * @author China.kxx
 * @date 2020/10/21 14:29
 */
@Slf4j
@Controller
@RequestMapping("/dataEntry/outsourcingPlan")
public class InputOutsourcingController extends BaseController {


    //页面文件路径
    private String prefix = "/dataEntry";
    @Autowired
    private InputOutsourcingService inputOutsourcingService;
    //加载页面
    @GetMapping("/manpower")
    public String manpower(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/manpower";
    }


//    @GetMapping
//    public String page(Model model) {
//        model.addAttribute("categories", inputOutsourcingService.findTypes());
//        return "dataEntry/outsourcingPlan";
//    }


    /**
     * 加载外购件列表数据
     * @param inputOutsourcingModel
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult manpower(InputOutsourcingModel inputOutsourcingModel) {
        List<InputOutsourcingModel> list = inputOutsourcingService.inputOutsourcingPlan(inputOutsourcingModel);
        return jsonResult(list);
    }
    /**
     * 导入
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/import")
    public JsonResult inputOutsourcingPlan(@RequestParam("file") MultipartFile file, InputOutsourcingModel inputOutsourcingModel) {
        return success(inputOutsourcingService.importFile(file, inputOutsourcingModel));
    }



    /**
     * 错误文件导出
     * @param url 文件路径
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/exist")
    public JsonResult checkFileExist(String url) {
        return jsonResult(inputOutsourcingService.exist(url));
    }
    /**
     * 外购件计划数据清空
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/delOutsourcing")
    public JsonResult delOutsourcing(InputOutsourcingModel inputOutsourcingModel) {
        int row = inputOutsourcingService.delOutsourcing(inputOutsourcingModel);
        if (row == -1){
            return error("外购件计划数据已被接收，无法进行删除！！需联系商务科");
        }else if(row == 0){
            return error("外购件计划无数据，操作失败");
        }else if(row == 1){
            return jsonResult(inputOutsourcingService.deleteOutsourcing(inputOutsourcingModel));
        }else {
            return JsonResult.fail();
        }
    }

}